#ifndef THREAD_HPP__
#define THREAD_HPP__
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <functional>

using namespace std;
using func_t = function<void(string&)>;
namespace thread_ns
{
    class Thread
    {
        void Excute()
        {
            _func(_threadname);
        }
    public:
        Thread(func_t func, const string &name)
        :_func(func),_threadname(name),_stop(true)
        {}

        static void *runthread(void *args)
        {
            Thread *self = static_cast<Thread*>(args);
            self->Excute();
            return nullptr;
        }
        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, runthread, this);
            if(!n)
            {
                _stop = false;
                return true;
            }
            else 
            {
                return false;
            }
        }

        void Stop()
        {
            _stop = false;
        }

        void Detach()
        {
            if(_stop)
            {
                pthread_detach(_tid);
            }
        }

        void Join()
        {
            pthread_join(_tid, nullptr);
        }

        ~Thread()
        {}

    private:
        pthread_t _tid;
        string _threadname;
        bool _stop;
        func_t _func;
    };
}

#endif